﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using IPCameraDll;
using MyCamerN;
using AForge.Video.FFMPEG;
using System.IO;
using System.Drawing;

namespace image_Pressimage
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        IPCamera ipcamer = null;
        VideoFileWriter vfw = new VideoFileWriter();
        private void btn_lv_xiang_Click(object sender, RoutedEventArgs e)
        {
            if (btn_lv_xiang.Content == "录像")
            {
                btn_lv_xiang.Content = "停止录像";
                ipcamer = new IpCameraHelper("192.168.10.5:81", "admin", "", new Action<ImageEventArgs>((args) =>
                {
                    images.Source = args.FrameReadyEventArgs.BitmapImage;
                    BitmapImage bitmap = (BitmapImage)images.Source;
                    vfw.Open("./lv/" + DateTime.Now.ToString() + ".avi", (int)images.Width, (int)images.Height, 30, VideoCodec.MPEG4);
                    vfw.WriteVideoFrame(bitmapimageTobitmap(bitmap));
                }));
                ipcamer.StartProcessing();
            }
            else {
                btn_lv_xiang.Content = "录像";
                vfw.Close();
                ipcamer.StopProcessing();
            }
           
        }
        //摄像头图片转成存储视频的图片
        public Bitmap bitmapimageTobitmap(BitmapImage bitmapimage) {
            using (MemoryStream ms=new MemoryStream())
            {
                BitmapEncoder enc = new BmpBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(bitmapimage));
                enc.Save(ms);
                Bitmap bitmap = new Bitmap(ms);
                return bitmap;

            }
        }
        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            

        }
    }
}
